//
//  LGHisReportTableCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/9/29.
//  his报告-table类型

#import "LGHisReportTableCell.h"
@interface LGHisReportTableCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)NSMutableArray *labelArray;
/**   */
@property (nonatomic , assign)CGFloat itemW;

@end

@implementation LGHisReportTableCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        [self.contentView addSubview:self.baseView];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(-viewPix(10));
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(viewPix(10));
            make.height.equalTo(@(0));
        }];
    }
    return  self;
}

-(void)setKeyArray:(NSArray *)keyArray{
    _keyArray = keyArray;
    for (UILabel *label in self.labelArray) {
        [label removeFromSuperview];
    }
    [self.labelArray removeAllObjects];
    
    NSInteger count = 0;
    for (NSDictionary *tempDic in keyArray) {
        NSString *content = LGNSString(tempDic[@"name"]);
        if (content.length == 0) {
            count++;
        }
    }
    CGFloat itemW = (Screen_W-viewPix(48)-viewPix(45)*count)/(keyArray.count-count);
    self.itemW = itemW;
    CGFloat left = viewPix(16);
    for (NSInteger i=0; i<keyArray.count; i++) {
        NSString *content = LGNSString(keyArray[i][@"name"]);
        CGFloat labelW = (content.length>0)?itemW:viewPix(45);
        UILabel *label = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:0];
        [self.baseView addSubview:label];
        [self.labelArray addObject:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.baseView).offset(left);
            make.centerY.equalTo(self.baseView);
            make.width.equalTo(@(labelW-viewPix(5)));
        }];
        left += labelW;
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    CGFloat maxH = viewPix(20);
    for (NSInteger i=0; i<self.keyArray.count; i++) {
        NSString *textColor = ([dataDic.allKeys containsObject:@"定性"] && LGNSString(dataDic[@"定性"]).length>0)?@"#DB453A":@"#333333";
        NSString *key = LGNSString(self.keyArray[i][@"key"]);
        NSString *content = LGNSString(dataDic[key]);
        CGFloat contentH = [content textHeightWithFont:LGFont(15) maxWidth:self.itemW-viewPix(5)];
        maxH = contentH>maxH?contentH:maxH;
        UILabel *label = self.labelArray[i];
        label.text = content;
        label.textColor = [UIColor colorWithString:textColor];
    }
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(maxH+viewPix(30)));
    }];
    
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(8), Screen_W-viewPix(32), viewPix(150));
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(NSMutableArray *)labelArray{
    if(!_labelArray){
        _labelArray = [NSMutableArray array];
    }
    return _labelArray;
}

@end
